
package com.myfoon.servlet.pub;


import com.myfoon.helper.CookieHelper;
import com.myfoon.helper.SessionHelper;
import com.myfoon.model.ejb.EventEJBLocal;
import com.myfoon.model.ejb.UserEJBLocal;
import com.myfoon.model.entity.Event;
import com.myfoon.model.entity.EventPhoto;
import com.myfoon.model.entity.Photo;
import com.myfoon.model.entity.User;
import com.myfoon.model.entity.result.EventOfFriend;
import com.myfoon.utils.CalendarUtils;
import com.myfoon.utils.JSONUtils;

import java.io.IOException;
import java.io.PrintWriter;

import java.sql.Timestamp;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import java.util.Map;

import javax.ejb.EJB;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;


public class Submit extends HttpServlet {        
    @EJB  
    EventEJBLocal eventLocal;

    @EJB
    UserEJBLocal userLocal;
    
    public Submit() {
    }

    @Override
    protected void doPost(HttpServletRequest httpServletRequest,
                          HttpServletResponse httpServletResponse) throws ServletException,
                                                                          IOException {     
        SessionHelper sessionHelper = new SessionHelper(httpServletRequest);
        Object attachedPhotosObject = httpServletRequest.getSession().getAttribute("attachedImages");
        List<Photo> attachedPhotos = null;
        
        if (attachedPhotosObject != null) {
            attachedPhotos = (List<Photo>)attachedPhotosObject;
        }
        else {
            attachedPhotos = new ArrayList<Photo>();
        }
         
        User user = sessionHelper.getUser();
        String message = httpServletRequest.getParameter("m");          
        
        if (user == null) {
            System.out.println("User is null");
            return;
        }
                
        if (message == null || message.equals("")) {   
            System.out.println("Message is null");
            return;                        
        }
        
        System.out.println(message);
        /* PARAMETERS START */
        String date_select = httpServletRequest.getParameter("ds");
        String date_year = "2009"; //httpServletRequest.getParameter("year_select");
        String date_month = httpServletRequest.getParameter("dm");
        String date_day = httpServletRequest.getParameter("dd");
        String time_hour = httpServletRequest.getParameter("th");
        String time_minute = httpServletRequest.getParameter("tm");
        /* ****************************/        
        
        List<EventOfFriend> eventsOfFriends = null;                
                
        Event event = new Event();                                
        Integer tzOffset = sessionHelper.getTimeZoneOffset();                
            
        event.setDate(new CalendarUtils().createDate(date_select, date_year, date_month, date_day, time_hour, time_minute, -1*tzOffset));                
        event.setOwner(user);
        event.setMessage(message);        
        
        List<EventPhoto> eventPhotos = new ArrayList<EventPhoto>();
        for (Photo photo : attachedPhotos) {
            EventPhoto e = new EventPhoto();
            e.setEvent(event);
            e.setPhoto(photo);
            eventPhotos.add(e);
        }
        
        event.setPhotos(eventPhotos);
        event = eventLocal.persist(event);
        httpServletRequest.getSession().setAttribute("attachedImages", new ArrayList<Photo>());
        eventsOfFriends = eventLocal.getLastEvents(user, tzOffset, true, 0, 15);                        
        
        PrintWriter writer = httpServletResponse.getWriter();
        JSONObject JSONConvertedEvents = new JSONUtils().convertFromEventOfFriends(eventsOfFriends);                                         
            
        httpServletResponse.setContentType("text/html; charset=UTF-8"); 
        httpServletResponse.setCharacterEncoding("UTF-8");
        writer.println(JSONConvertedEvents.toString());
        writer.flush();
        writer.close();
    }

    @Override
    protected void doGet(HttpServletRequest httpServletRequest,
                         HttpServletResponse httpServletResponse) throws ServletException,
                                                                         IOException {
        this.doPost(httpServletRequest, httpServletResponse);
    }
}
